<!DOCTYPE html>
<html>
<!--
  Copyright 2015 The Chromium Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.
-->
<head>
<title>IDB Test that we can store and retrieve an empty blob</title>
<script src="common.js"></script>
<script>

// Constants.
var store_name = 'empty_blob';
var blob_key = 'blob_key';
var file_key = 'file_key';

// Shared variables.
var db;
var emptyFileInput;
var testTimes = false;

function test() {
  param = location.hash.substring(1);
  if (param == 'ignoreTimes') {
    testTimes = false;
  }
  indexedDBTest(prepareDatabase, writeBlob);
}

function prepareDatabase() {
  db = event.target.result;
  db.createObjectStore(store_name);
}

function writeBlob() {
  debug("Writing blob.");

  put_blob = new Blob();

  var put_tx = db.transaction(store_name, 'readwrite');
  put_tx.onabort = unexpectedAbortCallback;
  var put_request = put_tx.objectStore(store_name).put(put_blob, blob_key);
  put_request.onerror = unexpectedErrorCallback;
  put_tx.oncomplete = function () {
    debug('Write complete');
    var get_tx = db.transaction(store_name);

    var get_request1 = get_tx.objectStore(store_name).get(blob_key);
    get_request1.onerror = unexpectedErrorCallback;
    get_request1.onsuccess = function(evt) {
      get_blob1 = evt.target.result;
      resultType = typeof get_blob1;
      shouldBe('resultType', '"object"');
      shouldBe('get_blob1.size', 'put_blob.size');
    };

    var get_request2 = get_tx.objectStore(store_name).get(blob_key);
    get_request2.onerror = unexpectedErrorCallback;
    get_request2.onsuccess = function(evt) {
      get_blob2 = evt.target.result;
      resultType = typeof get_blob2;
      shouldBe('resultType', '"object"');
      shouldBe('get_blob2.size', 'put_blob.size');
    };

    get_tx.onabort = unexpectedAbortCallback;
    get_tx.oncomplete = writeFile;
  };
}

function writeFile() {
  debug('Writing file.');
  put_file = new File([''], 'somefile', {
      type: 'application/x-special-snowflake',
      lastModified: new Date('1999-12-31T23:59:59Z')
  });
  var put_tx = db.transaction(store_name, 'readwrite');
  put_tx.objectStore(store_name).put(put_file, file_key);
  put_tx.onabort = unexpectedAbortCallback;
  put_tx.oncomplete = function() {
    var get_tx = db.transaction(store_name);
    get_tx.objectStore(store_name).get(file_key).onsuccess = function(e) {
      get_file = e.target.result;

      shouldBe('get_file.name', 'put_file.name');
      shouldBe('get_file.size', 'put_file.size');
      shouldBe('get_file.type', 'put_file.type');
      if (testTimes) {
        shouldBe('get_file.lastModified', 'put_file.lastModified');
        shouldBe('String(get_file.lastModifiedDate)', 'String(put_file.lastModifiedDate)');
      }
    };
    get_tx.onabort = unexpectedAbortCallback;
    get_tx.oncomplete = deleteFile;
  };
}

function deleteFile() {
  var tx = db.transaction(store_name, 'readwrite');
  tx.objectStore(store_name).delete(file_key);
  tx.onabort = unexpectedAbortCallback;
  tx.oncomplete = deleteBlob;
}

function deleteBlob() {
  var tx = db.transaction(store_name, 'readwrite');
  tx.objectStore(store_name).delete(blob_key);
  tx.onabort = unexpectedAbortCallback;
  tx.oncomplete = done;
}

</script>
</head>
<body onLoad="test()">
  <div id="status">Starting...</div>
</body>
</html>
